package com.eeepay.eeepay_v2.api;

import android.support.annotation.NonNull;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ActivityCenterServiceGrpc;
import cn.eeepay.api.grpc.nano.ManagementCenterProto;
import io.grpc.ManagedChannel;

/**
 * 描述：获取活动补贴明细
 * 作者：zhuangzeqin
 * 时间: 2017/6/28-10:36
 * 邮箱：zzq@eeepay.cn
 */
public class SubsidiesDetailRecordAPI {
    private static final int GETACTIVITYSUBSIDIESDETAIL = 1;//获得活动账号记录信息请求任务id

    private ResultCallBack resultCallBack;//设置监听接口

    private int mTotal;//总的记录数

    public final static int PAGESIZE = 10;//每页显示10条

    public SubsidiesDetailRecordAPI(){}

    /**
     * 设置监听回调
     *
     * @param resultCallBack
     */
    public void setResultCallBack(@NonNull ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 获取活动补贴明细
     */
    public void getSubsidiesRecordInfo(final int currPage) {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        if (currPage==0)
        {
            throw new RuntimeException("currPage is not zero.");
        }
        resultCallBack.oninit();//请求前做一些初始化操作
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, GETACTIVITYSUBSIDIESDETAIL, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                ActivityCenterServiceGrpc.ActivityCenterServiceBlockingStub stub = ActivityCenterServiceGrpc.newBlockingStub(channel).withDeadlineAfter(Constant.grpc_time_out, TimeUnit.SECONDS);
                ManagementCenterProto.ActivitySubsidiesDetailReq req = new ManagementCenterProto.ActivitySubsidiesDetailReq();
                req.userId = UserInfo.getUserInfo2SP().getAgentNo();
                req.page = currPage;
                req.pageSize = PAGESIZE;
                return stub.getActivitySubsidiesDetail(req);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                resultCallBack.onUnInit();//返初始化操作
                if (result == null) {
                    resultCallBack.onFail("查询账户记录失败");
                    return;
                }
                ManagementCenterProto.ActivitySubsidiesDetailRes res = (ManagementCenterProto.ActivitySubsidiesDetailRes)result;
                if (!res.resStatus.status) {
                    resultCallBack.onFail(res.resStatus.msg);
                    return;
                }
                mTotal = res.total;
                if (mTotal==0)
                {
                    resultCallBack.onFail("暂时没有相关提现记录,请稍后重试.");
                    return;
                }
                ManagementCenterProto.ActivitySubsidiesDetail[] subsidiesDetails = res.list;
                resultCallBack.onSucess(currPage,mTotal,Arrays.asList(subsidiesDetails));
            }
        });
    }

    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void oninit();//初始化

        void onFail(String msg);//失败返回；提示语

        void onSucess(int currPage,int total,List<ManagementCenterProto.ActivitySubsidiesDetail> subsidiesDetails);//成功返回列表数据数据

        void onUnInit();//返初始化操作
    }
}
